package com.greystripe.sdk;

import android.content.Context;
import android.location.Location;
import android.webkit.CookieManager;
import android.webkit.WebViewDatabase;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Timer;

abstract class c
  implements DeviceInfo.LocationListener, GSAd
{
  protected boolean a = false;
  protected boolean b = false;
  private final long c = 30000L;
  private Context d;
  private aa e;
  private v f = new v(this, (byte)0);
  private r g = new r(this, (byte)0);
  private ArrayList h;
  private Timer i;
  private a j;
  private WeakReference k;
  private boolean l = false;
  private boolean m = false;
  private boolean n = false;
  private boolean o = false;
  private boolean p = false;
  private boolean q = false;
  private s r;
  private boolean s = false;
  private boolean t = false;
  private boolean u = false;
  private int v = -1;

  c(Context paramContext)
  {
    bm.a();
    this.v = -1;
    this.d = paramContext.getApplicationContext();
    bj.a(paramContext);
    String str = DeviceInfo.getDeviceInfoController(paramContext).b();
    if (str != null)
      bj.a(str);
    a("guid", bj.b());
    if (!bj.b(bk.AD_SERVER_URL))
      bj.b("http://adsx.greystripe.com/openx/www/delivery/ia.php");
    bd.b("adServerUrl: " + bj.c(), new Object[0]);
    this.e = new aa(this.d, new p(this, (byte)0));
    DeviceInfo localDeviceInfo = DeviceInfo.getDeviceInfoController(this.d);
    this.e.addJavascriptInterface(new m(this, (byte)0), "AdState");
    this.r = new s(this, (byte)0);
    this.e.addJavascriptInterface(this.r, "MraidController");
    this.e.addJavascriptInterface(localDeviceInfo, "DeviceInfo");
    this.e.addJavascriptInterface(bh.a(this.d), "NetworkStatus");
    this.j = new a(this.d, this.e);
    this.e.addJavascriptInterface(this.j, "Accelerometer");
    a("hid", localDeviceInfo.getHashedAndroidId("SHA-1"));
    a("mhid", localDeviceInfo.getHashedAndroidId("MD5"));
    this.g.add("port");
    this.g.add("land");
    this.g.add("asdk-" + GSSdkInfo.getVersion());
    this.g.add(localDeviceInfo.getAndroidVersion());
    if (localDeviceInfo.hasTelephony())
      this.g.add("phone");
    if (localDeviceInfo.isTablet())
      this.g.add("tablet");
    if (this.d.checkCallingOrSelfPermission("android.permission.CAMERA") == 0)
      this.g.add("camera");
    if (this.d.checkCallingOrSelfPermission("android.permission.WRITE_CONTACTS") == 0)
      this.g.add("contacts");
    if (this.d.checkCallingOrSelfPermission("android.permission.WRITE_CALENDAR") == 0)
      this.g.add("calendar");
    a("lang", localDeviceInfo.getLanguage());
    a("screen_size", localDeviceInfo.getWidthPixels() + "x" + localDeviceInfo.getHeightPixels());
    a("res", Float.toString(localDeviceInfo.getDensity()));
    DeviceInfo.addLocationListener(this);
  }

  private void k()
  {
    this.n = false;
    this.m = false;
    this.l = false;
    this.o = false;
    this.b = false;
  }

  private void l()
  {
    bm.a(new d(this));
  }

  private GSAd m()
  {
    if ((this.k != null) && (this.k.get() != null))
      this = (GSAd)this.k.get();
    return this;
  }

  private void n()
  {
    if (this.t);
    do
    {
      return;
      i();
    }
    while (this.h == null);
    bm.a(new f(this, m()));
  }

  private void o()
  {
    try
    {
      bd.b("AdModel#onDisplay", new Object[0]);
      this.o = true;
      if (this.l)
        bd.b("Was fetching an ad when tried to notify ondisplay.", new Object[0]);
      while (true)
      {
        return;
        if (this.n)
        {
          this.p = true;
          bd.b("DISPLAY DISPLAY DISPLAY", new Object[0]);
          k();
          this.e.b("EventHandler.broadcastEvent('displayed');");
        }
      }
    }
    finally
    {
    }
  }

  private void p()
  {
    try
    {
      if (this.i != null)
      {
        bd.b("STOPPING READY timer.", new Object[0]);
        this.i.cancel();
        this.i = null;
      }
      return;
    }
    finally
    {
      localObject = finally;
      throw localObject;
    }
  }

  abstract void a();

  protected final void a(GSAdErrorCode paramGSAdErrorCode)
  {
    if (this.h == null)
      return;
    bm.a(new g(this, m(), paramGSAdErrorCode));
  }

  final void a(am paramam)
  {
    this.k = new WeakReference(paramam);
  }

  protected final void a(String paramString1, String paramString2)
  {
    if ((paramString1 == null) || (paramString2 == null))
      return;
    this.f.put(paramString1, paramString2);
  }

  public void addListener(GSAdListener paramGSAdListener)
  {
    if (this.h == null)
      this.h = new ArrayList();
    this.h.add(paramGSAdListener);
  }

  abstract void b();

  abstract void c();

  final void d()
  {
    this.j.stop();
  }

  final boolean e()
  {
    return this.p;
  }

  final void f()
  {
    this.e.b("EventHandler.broadcastEvent('imagedidsave');");
  }

  protected final void g()
  {
    bd.d("An ad is cached and ready to display. Cannot request another ad at this time.", new Object[0]);
    n();
  }

  public int getId()
  {
    return this.v;
  }

  protected void h()
  {
    while (true)
    {
      try
      {
        bd.b("requestAd", new Object[0]);
        boolean bool = bj.g();
        int i1 = 0;
        if (bool)
        {
          if (i1 == 0)
          {
            bd.e("Please de-integrate the Greystripe SDK from your app. Its ad requests are no longer being honored.", new Object[0]);
            a(GSAdErrorCode.UNEXPECTED_ERROR);
          }
        }
        else
        {
          String str = CookieManager.getInstance().getCookie(bj.c() + '?' + this.f.a());
          if (str == null)
          {
            i1 = 1;
            continue;
          }
          if (str.indexOf("SNOOKI") == -1)
          {
            i1 = 1;
            continue;
          }
          bj.f();
          i1 = 0;
          continue;
        }
      }
      finally
      {
      }
      if (!bj.b(bk.GUID))
      {
        bd.e("Missing application identifier GUID.", new Object[0]);
        a(GSAdErrorCode.INVALID_APPLICATION_IDENTIFIER);
      }
      else if (this.n)
      {
        g();
      }
      else if (this.l)
      {
        bd.d("Currently fetching an ad. Cannot request another ad at this time.", new Object[0]);
      }
      else
      {
        p();
        this.v = -1;
        k();
        l();
        bh localbh = bh.a(this.d);
        if (!localbh.a())
        {
          a(GSAdErrorCode.NO_NETWORK);
        }
        else
        {
          this.l = true;
          this.p = false;
          this.q = false;
          this.t = false;
          this.u = false;
          if (localbh.b())
            this.g.add("wifi");
          while (true)
          {
            Location localLocation = DeviceInfo.a();
            if (localLocation != null)
            {
              a("lat", Double.toString(localLocation.getLatitude()));
              a("long", Double.toString(localLocation.getLongitude()));
              a("alt", Double.toString(localLocation.getAltitude()));
              a("gpsacy", Float.toString(localLocation.getAccuracy()));
              a("gpstime", Long.toString(localLocation.getTime()));
            }
            a("source", this.g.a());
            this.e.stopLoading();
            if (WebViewDatabase.getInstance(this.d) != null)
              break label429;
            a(GSAdErrorCode.UNEXPECTED_ERROR);
            break;
            if (this.g.contains("wifi"))
              this.g.remove("wifi");
          }
          label429: this.e.a(bj.c() + '?' + this.f.a());
        }
      }
    }
  }

  protected final void i()
  {
    bm.a(new e(this));
  }

  public boolean isAdReady()
  {
    if (!bh.a(this.d).a())
      bd.b("Network status: not connected", new Object[0]);
    while ((!this.m) || (!this.n))
      return false;
    return true;
  }

  protected final aa j()
  {
    return this.e;
  }

  public void onLocationUpdated(Location paramLocation)
  {
    StringBuilder localStringBuilder = new StringBuilder();
    localStringBuilder.append("LocationHandler.updateLocation(");
    localStringBuilder.append(paramLocation.getLatitude());
    localStringBuilder.append(", ");
    localStringBuilder.append(paramLocation.getLongitude());
    localStringBuilder.append(", ");
    localStringBuilder.append(paramLocation.getAltitude());
    localStringBuilder.append(", ");
    localStringBuilder.append(paramLocation.getAccuracy());
    localStringBuilder.append(", ");
    localStringBuilder.append(paramLocation.getTime());
    localStringBuilder.append(");");
    this.e.b(localStringBuilder.toString());
  }

  public void removeListener(GSAdListener paramGSAdListener)
  {
    if (this.h != null)
      this.h.remove(paramGSAdListener);
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.greystripe.sdk.c
 * JD-Core Version:    0.6.2
 */